package com.jzy.message.util.common;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;


public class SharedUtils {
    public static String FILE_NAME = "openvideo";
    private static final String MODE_NAME = "welcome";

    public static void putWelcomeBoolean(Context context, boolean isFirst) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putBoolean(MODE_NAME, isFirst);
        editor.commit();
    }

    public static boolean getWelcomeBoolean(Context context) {
        return context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE).
                getBoolean(MODE_NAME, false);
    }

    public static void putRememberPasswordBoolean(Context context, boolean isRemember) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putBoolean("rememberPass", isRemember);
        Log.d("sharedUtils", "saved rememberPass " + isRemember);
        editor.commit();
    }

    public static boolean getRememberPasswordBoolean(Context context) {
        return context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE).
                getBoolean("rememberPass", false);
    }


    public static void putPushBoolean(Context context, boolean pushEnabled) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putBoolean("push", pushEnabled);
        editor.commit();
    }

    public static boolean getPushBoolean(Context context) {
        return context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE).
                getBoolean("push", true);
    }


    public static boolean getLoginBoolean(Context context) {
        return context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE).
                getBoolean("login", false);
    }

    public static void putLoginBoolean(Context context, boolean isLogin) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putBoolean("login", isLogin);
        editor.commit();
    }


    public static String getCityName(Context context) {
        return context.getSharedPreferences(FILE_NAME,
                context.MODE_PRIVATE).getString("cityName", "选择城市");
    }


    public static String getTempre(Context context) {
        return context.getSharedPreferences(FILE_NAME,
                context.MODE_PRIVATE).getString("tempre", "温度");
    }

    //写入password
    public static void putPassword(Context context, String password) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("password", password);
        Log.d("SharedUtils", "saved password " + password);
        editor.commit();
    }

    public static String getPassword(Context context) {

        Log.d("SharedUtils", "get password " + context.getSharedPreferences(FILE_NAME,
                context.MODE_PRIVATE).getString("password", ""));

        return context.getSharedPreferences(FILE_NAME,
                context.MODE_PRIVATE).getString("password", "");
    }

    //写入latitude
    public static void putLatitude(Context context, String latitude) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("latitude", latitude);
        editor.commit();
    }

    public static String getLatitude(Context context) {


        return context.getSharedPreferences(FILE_NAME,
                context.MODE_PRIVATE).getString("latitude", "");
    }

    //写入longitude
    public static void putlongitude(Context context, String latitude) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("longitude", latitude);
        editor.commit();
    }

    public static String getlongitude(Context context) {


        return context.getSharedPreferences(FILE_NAME,
                context.MODE_PRIVATE).getString("longitude", "");
    }

    //写入Cookie

    public static void putUserCookie(Context context, String Cookie) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("Cookie", Cookie);
        editor.commit();
    }


    //获取token
    public static String getUserCookie(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString("Cookie", "");
    }
    //写入userId

    public static void putUserId(Context context, String userId) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("userId", userId);
        editor.commit();
    }


    //获取userId
    public static String getUserId(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString("userId", "");
    }
    //写入userNickname

    public static void putUserNickname(Context context, String userId) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("nickname", userId);
        editor.commit();
    }


    //获取userNickname
    public static String getUserNickname(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString("nickname", "");
    }


    //获取birthday
    public static String getUserBirthday(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString("birthday", "");
    }
    //写入CurrentDoorplateUUID

    public static void putCurrentDoorplateUUID(Context context, String CurrentDoorplateUUID) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("CurrentDoorplateUUID", CurrentDoorplateUUID);
        editor.commit();
    }
    //写入CurrentCommunityUUID

    public static void putCurrentCommunityUUID(Context context, String CurrentCommunityUUID) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("CurrentCommunityUUID", CurrentCommunityUUID);
        editor.commit();
    }
    //写入CurrentAddress

    public static void putCurrentAddress(Context context, String CurrentAddress) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("CurrentAddress", CurrentAddress);
        editor.commit();
    }

    //写入birthday

    public static void putUserBirthday(Context context, String value) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("birthday", value);
        editor.commit();
    }

    //写入登录名称

    public static void putUserName(Context context, String userName) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("userName", userName);
        editor.commit();
    }

    //写入登录手机号

    public static void putUserMobile(Context context, String userMobile) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("userMobile", userMobile);
        editor.commit();
    }

    //获取登录手机号
    public static String getUserMobile(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString("userMobile", "");
    }
    //写入xmpp登录状态

    public static void putXmppLoggined(Context context, boolean logined) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putBoolean("xmppLoggined", logined);
        editor.commit();
    }

    //获取xmpp登录状态
    public static boolean getXmppLoggined(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getBoolean("xmppLoggined", false);
    }
    //写入记住密码状态

    public static void putRememberPass(Context context, boolean remembered) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putBoolean("rememberPass", remembered);
        editor.commit();
    }

    //获取记住密码状态
    public static boolean getRememberPass(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getBoolean("rememberPass", false);
    }


    //写入登录图片地址
    public static void putUserImage(Context context, String userImageUrl) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, Context.MODE_APPEND).edit();
        editor.putString("userImageUrl", userImageUrl);
        editor.commit();
    }


    //获取CurrentDoorplateUUID

    public static String getCurrentDoorplateUUID(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString("CurrentDoorplateUUID", "");
    }
    //获取CurrentCommunityUUID

    public static String getCurrentCommunityUUID(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString("CurrentCommunityUUID", "");
    }

    //获取CurrentAddress

    public static String getCurrentAddress(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString("CurrentAddress", "请设置当前地址");
    }


    //获取登录名称

    public static String getUserName(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString("userName", "点击登录");
    }


    //获取登录photo url
    public static String getUserImage(Context context) {
        return context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString("userImageUrl", "assets/empty_images.jpg");
    }

    public static void putCityName(Context context, String cityName) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, context.MODE_APPEND).edit();
        editor.putString("cityName", cityName);
        editor.commit();
    }

    public static void putTempre(Context context, String tempre) {
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, context.MODE_APPEND).edit();
        editor.putString("tempre", tempre);
        Log.d("abc", tempre);
        editor.commit();
    }


    public static void putBitmapToSharedPreferences(Bitmap bitmap, Context context, String image) {

        //第一步:将Bitmap压缩至字节数组输出流ByteArrayOutputStream
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, byteArrayOutputStream);
        //第二步:利用Base64将字节数组输出流中的数据转换成字符串String
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        String imageString = new String(Base64.encodeToString(byteArray, Base64.DEFAULT));
        //第三步:将String保持至SharedPreferences
        SharedPreferences.Editor editor = context.getSharedPreferences(FILE_NAME, context.MODE_APPEND).edit();
        editor.putString(image, imageString);
        editor.commit();
    }

    public static Bitmap getBitmapFromSharedPreferences(Context context, String image) {
        //第一步:取出字符串形式的Bitmap
        String imageString = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).getString(image, "");
        //第二步:利用Base64将字符串转换为ByteArrayInputStream
        byte[] byteArray = Base64.decode(imageString, Base64.DEFAULT);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
        //第三步:利用ByteArrayInputStream生成Bitmap
        Bitmap bitmap = BitmapFactory.decodeStream(byteArrayInputStream);
        return bitmap;
    }

    public static void myToast(Context context, String s) {
        Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
    }
}
